﻿/* ==============================================================================
 * 功能描述：    调用电脑摄像头，并进行拍摄、保存。
 * 创 建 者：    泰勒Peano
 * 交流邮箱：    goodzheng@88.com
 * 交流QQ：      656029714
 * 创建日期：    2020.09.04
 *.Net Version  3.5
 * ==============================================================================*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AForge;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;

using Camera_002;
namespace Camera_001
{
    public partial class Form1 : Form
    {
        #region 变量

        FilterInfoCollection videoDevices;//摄像头设备集合
        VideoCaptureDevice videoSource;//捕获设备源
        Bitmap img;//处理图片
        private Timer timer;
        private int timernum = 0;
        private int metnum = 0;
        #endregion

        #region 构造函数
        public Form1()
        {
            InitializeComponent();
            // 初始化一个5分钟间隔的计时器  
            timer = new Timer(); // 300000 毫秒 = 5分钟  
            timer.Interval = 30*1000;
            timer.Tick += Timer_Tick;
            timer.Enabled = true;
            timer.Start();
        }
        #endregion

        #region 事件
        private void Form1_Load(object sender, EventArgs e)
        {
            //先检测电脑所有的摄像头
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            // 假设"123"是下拉列表中的某个项，并且您知道它在列表中的索引位置  
            comboBox1.Items.Clear();
            for (int i = 0; i < videoDevices.Count; i++)
            {
                int j = i + 1;
                comboBox1.Items.Add("摄像头" + j.ToString());
            }
            if (videoDevices.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
            }
            if (videoDevices.Count != 1)
            {
                MessageBox.Show("检测到了" + videoDevices.Count.ToString() + "个摄像头！");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShutCamera();//保证释放摄像头
            if (comboBox1.Text == "摄像头1" && videoDevices.Count > 0)
                videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            else if (comboBox1.Text == "摄像头2" && videoDevices.Count > 1)
                videoSource = new VideoCaptureDevice(videoDevices[1].MonikerString);
            else
            {
                MessageBox.Show("选择的摄像头不存在！！！");
                return;
            }
            videoSourcePlayer1.VideoSource = videoSource;
            videoSourcePlayer1.Start();

            button1.Enabled = true;//开启“拍摄功能”
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ShutCamera();//保证释放摄像头

        }

        private void button1_Click(object sender, EventArgs e)
        {

            img = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄
            pictureBox1.Image = img;
            button2.Enabled = true;//开启“保存”功能

        }

        //"保存"按钮click事件
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //以当前时间为文件名，保存为jpg格式
                //图片路径在程序bin目录下的Debug下
                TimeSpan tss = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                long a = Convert.ToInt64(tss.TotalMilliseconds) / 1000;  //以秒为单位

                button2.Enabled = false;
                Class1 ident = new Class1();
                ident.RunBit(img);
                bool saveit = false;
                if (saveit)
                {
                    String saved = string.Format("{0}.jpg", a.ToString());
                    img.Save(saved);
                    ident.Run(saved);
                    MessageBox.Show("图片保存成功，注意隐私。");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region 方法
        // 关闭并释放摄像头
        public void ShutCamera()
        {
            if (videoSourcePlayer1.VideoSource != null)
            {
                videoSourcePlayer1.SignalToStop();
                videoSourcePlayer1.WaitForStop();
                videoSourcePlayer1.VideoSource = null;
            }
        }
        #endregion
        private void Timer_Tick(object sender, EventArgs e)
        {
            // 取消按键自由
            button1.Enabled = false;
            button2.Enabled = false;
            // 在这里编写需要定期执行的代码
            // 拍照
            img = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄
            pictureBox1.Image = img;
            timernum++;
            
            // 识别
            bool iscat = false;
            try
            {
                Class1 ident = new Class1();
                iscat = ident.RunSlient(img);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            // 回传识别结果
            if (iscat)
            {
                // 采取行动
                //关闭相机---whocare
                //关闭定时器--whocare
                //摇5分钟花手
                dangce();
                metnum++;
            }

            //不管是不是采取行动
            //恢复按键自由
            button1.Enabled = true;
            button2.Enabled = false;
            catcounter.Text = String.Format("{0}:{1}",metnum, timernum);
        }

        private void dangce()
        {
            //dance since I found the cat want to jump kemu3;
        }
    }

}
